﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace LdtJudge.DataEngine.Common
{
    public class HttpClientService
    {
        private readonly HttpClient _httpClient;

        public HttpClientService()
        {
            // HttpClient实例应当被重用，而不是每次请求时都创建新的实例
            _httpClient = new HttpClient();
        }

        public async Task<string> PostAsync(string url, string jsonContent)
        {
            // 将JSON内容作为字符串内容
            var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            // 发送POST请求
            HttpResponseMessage response = await _httpClient.PostAsync(url, content);

            // 确保请求成功
            response.EnsureSuccessStatusCode();

            // 读取响应内容
            string responseBody = await response.Content.ReadAsStringAsync();

            // 返回响应内容
            return responseBody;
        }
    }
}
